<?php

namespace App\Http\Controllers;

use App\Constant;
use App\Models\BalanceLogModel;
use App\Models\ManualDfMethod;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\SellerWithdrawRequest;
use Auth;
use App\Models\Seller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class SellerWithdrawRequestController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $seller_withdraw_requests = SellerWithdrawRequest::where('user_id', Auth::user()->id)::latest()->paginate(9);
        return view('frontend.user.seller.seller_withdraw_requests.index', compact('seller_withdraw_requests'));
    }

    public function request_index()
    {
        $seller_withdraw_requests = SellerWithdrawRequest::with(['payment'])->where('user_type', 'seller')->latest()->paginate(15);
        return view('backend.sellers.seller_withdraw_requests.index', compact('seller_withdraw_requests'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        $seller = User::where('id', Auth::user()->id)->lockForUpdate()->firstOrFail();
        if( empty($seller->bank_name) && (empty($seller->bank_acc_name) || empty($seller->bank_acc_no) || empty($seller->bank_routing_no)))
        {
            flash(translate('Please fill in your profile'))->error();
            return back();
        }
        if (!Hash::check($request->pay_password, $seller->pay_password)) {
            flash(translate('Pay Password Error.'))->error();
            return redirect()->back();
        }
        if( $seller->balance < $request->amount)
        {
            flash(translate('Money is not enough'))->error();
            return back();
        }

        if($request->amount < 500)
        {
            flash(translate('Minimum 500 cash withdrawal amount'))->error();
            return back();
        }
        try {
            DB::beginTransaction();

            $seller_withdraw_request = new SellerWithdrawRequest;
            $seller_withdraw_request->user_id = Auth::user()->id ;
            $seller_withdraw_request->user_type = Auth::user()->user_type;
            $seller_withdraw_request->amount = $request->amount;
            $seller_withdraw_request->message = $request->message;
            $seller_withdraw_request->status = '0';
            $seller_withdraw_request->viewed = '0';
            $seller_withdraw_request->save();

            $balance_log_model = BalanceLogModel::create([
                'user_id' => $seller->id,
                'type' => Constant::BALANCE_LOG_TYPE_WITHDRAW,
                'value_before' => $seller->balance,
                'value' => $request->amount,
                'description' => "Withdraw Apply",
                'trade_id' => $seller_withdraw_request->id,
            ]);

            // 扣减余额
            $seller->fill(['balance' => ($seller->balance - $request->amount)])->save();

            DB::commit();
            flash(translate('Request has been sent successfully'))->success();
            return redirect()->route('wallet.index');
        } catch (\Exception $e) {
            DB::rollBack();
            flash(translate('Something went wrong'))->error();
            return back();
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function payment_modal(Request $request)
    {
        $seller = Seller::where('user_id', $request->id)->first();
        $seller_withdraw_request = SellerWithdrawRequest::where('id', $request->seller_withdraw_request_id)->first();
        $channel = ManualDfMethod::all();
        return view('backend.sellers.seller_withdraw_requests.payment_modal', compact('seller', 'seller_withdraw_request', 'channel'));
    }

    public function message_modal(Request $request)
    {
        $seller_withdraw_request = SellerWithdrawRequest::findOrFail($request->id);
        if (Auth::user()->user_type == 'seller') {
            return view('frontend.partials.withdraw_message_modal', compact('seller_withdraw_request'));
        }
        elseif (Auth::user()->user_type == 'admin' || Auth::user()->user_type == 'staff') {
            return view('backend.sellers.seller_withdraw_requests.withdraw_message_modal', compact('seller_withdraw_request'));
        }
    }
}
